package com.starmark.sys.org.atomic.command;



import com.starmark.sys.org.atomic.constants.SysOrgConstant;
import com.starmark.sys.org.atomic.entity.SysOrgUser;
import com.starmark.sys.org.atomic.service.ISysOrgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Order(value = 2)
public class SysOrgUserInitCommand implements CommandLineRunner {

    @Autowired
    ISysOrgUserService sysOrgUserService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(String... args) {
        SysOrgUser admin = sysOrgUserService.getById(SysOrgConstant.ADMIN_USER_ID);
        if (admin == null) {
            admin = new SysOrgUser();
            admin.setLoginName("admin");
            admin.setName("超级管理员");
            admin.setId(SysOrgConstant.ADMIN_USER_ID);
            admin.setPassword("123456");
            admin.setStatus(true);
            admin.setCreateBy(SysOrgConstant.ADMIN_USER_ID);
            admin.setRemove(false);
            sysOrgUserService.save(admin);
        }
        //创建匿名用户
        SysOrgUser anonymous = sysOrgUserService.getById(SysOrgConstant.ANONYMOUS_USER_ID);
        if (anonymous == null) {
            anonymous = new SysOrgUser();
            anonymous.setLoginName(SysOrgConstant.ANONYMOUS_USER_NAME);
            anonymous.setName("匿名用户");
            anonymous.setId(SysOrgConstant.ANONYMOUS_USER_ID);
            anonymous.setPassword(SysOrgConstant.ANONYMOUS_USER_PASSWORD);
            anonymous.setStatus(true);
            anonymous.setCreateBy(SysOrgConstant.ADMIN_USER_ID);
            admin.setRemove(false);
            sysOrgUserService.save(anonymous);
        }

        //创建系统用户
        SysOrgUser system = sysOrgUserService.getById(SysOrgConstant.INNER_SYSTEM_ID);
        if (system == null) {
            system = new SysOrgUser();
            system.setLoginName("");
            system.setName("系统用户");
            system.setId(SysOrgConstant.INNER_SYSTEM_ID);
            system.setPassword("");
            system.setStatus(true);
            system.setCreateBy(SysOrgConstant.ADMIN_USER_ID);
            system.setRemove(false);
            sysOrgUserService.save(system);
        }
    }
}
